package com.gopay.common.cps.manager.gsrt.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.gopay.common.cps.dao.gsrt.GsrtRepaymentDao;
import com.gopay.common.cps.manager.gsrt.GsrtRepaymentQueryManager;
import com.gopay.common.domain.gsrt.GsrtCustBid;
import com.gopay.common.domain.gsrt.GsrtRepayment;

@Repository("gsrtRepaymentQueryManager")
public class GsrtRepaymentQueryManagerImpl implements GsrtRepaymentQueryManager {

	@Resource(name = "gsrtRepaymentDao")
	private GsrtRepaymentDao gsrtRepaymentDao;
	
	 private final String FIND_TIMEOUT_ORDER_BY_TIMESPAN_SQL = "SELECT  c.id  FROM GSRT_REPAYMENT C "
				+ "  where(C.TXN_STA_CD = '30001'"
				+ " AND ROUND(TO_NUMBER(SYSDATE - (C.CREATED_TS+0))*24*60) >= :timeSpan) ";
	    

	
	@Override
	public List<GsrtRepayment> getByGopayId(String gopayOrderId) {
		return gsrtRepaymentDao.getByGopayId(gopayOrderId);
	}

	@Override
	public List<GsrtRepayment> getByMerIdOrderId(String merId, String orderId) {
		return gsrtRepaymentDao.getByMerIdOrderId(merId, orderId);
	}
	
	
	@Override
    public GsrtRepayment getByMerIdorgOrderId(String merId, String merOrderId){
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("merCustId", merId);
		paramMap.put("merOrderId", merOrderId);
		String hql="from GsrtRepayment where merCustId=:merCustId and merOrderId=:merOrderId";
		return (GsrtRepayment) (gsrtRepaymentDao.findByHql(hql,paramMap).size()>0?gsrtRepaymentDao.findByHql(hql,paramMap).get(0):null);
		
	}
	
    /**
     * 根据网关流水号查询
     */
    @Override
    public GsrtRepayment findByTransDtlSq(Long transDtlSq){
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("transDtlSq", transDtlSq);
		String hql="from GsrtRepayment where transDtlSq=:transDtlSq ";
		return (GsrtRepayment) (gsrtRepaymentDao.findByHql(hql,paramMap).size()>0?gsrtRepaymentDao.findByHql(hql,paramMap).get(0):null);
		
	}

	@Override
	public List<Object> findTimeoutListByTimeSpan(int timeSpan) {
		Map m = new HashMap();
		m.put("timeSpan", timeSpan);
		List<?> result = gsrtRepaymentDao.findBySql(FIND_TIMEOUT_ORDER_BY_TIMESPAN_SQL, m);
		return (List<Object>) result;
	}
	
	

}
